using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Xsl;
using System.Xml;
using System.Text.RegularExpressions;

namespace XsltHelper
{
	public partial class MainForm : Form
	{
		public MainForm() {
			InitializeComponent();
		}

		private void btnInputSave_Click(object sender, EventArgs e) {
			this.dlgSave.DefaultExt = "xml";
			this.dlgSave.Filter = "Xml file (*.xml)|*.xml";
			if (this.dlgSave.ShowDialog() == DialogResult.OK) {
				using (StreamWriter writer = new StreamWriter(this.dlgSave.FileName)) {
					writer.Write(this.txtInput.Text);
					writer.Flush();
				}
			}
		}

		private void btnXsltSave_Click(object sender, EventArgs e) {
			this.dlgSave.DefaultExt = "xsl";
			this.dlgSave.Filter = "Xsl file (*.xsl)|*.xsl";
			if (this.dlgSave.ShowDialog() == DialogResult.OK) {
				using (StreamWriter writer = new StreamWriter(this.dlgSave.FileName)) {
					writer.Write(this.txtXslt.Text);
					writer.Flush();
				}
			}
		}

		private void btnOutputSave_Click(object sender, EventArgs e) {
			this.dlgSave.DefaultExt = "xml";
			this.dlgSave.Filter = "Xml file (*.xml)|*.xml";
			if (this.dlgSave.ShowDialog() == DialogResult.OK) {
				using (StreamWriter writer = new StreamWriter(this.dlgSave.FileName)) {
					writer.Write(this.txtOutput.Text);
					writer.Flush();
				}
			}
		}

		private void btnInputLoad_Click(object sender, EventArgs e) {
			this.dlgOpen.DefaultExt = "xml";
			this.dlgOpen.Filter = "Xml file (*.xml)|*.xml";
			if (this.dlgOpen.ShowDialog() == DialogResult.OK) {
				using (StreamReader reader = new StreamReader(this.dlgOpen.FileName)) {
					this.txtInput.Text = reader.ReadToEnd();
				}
			}
		}

		private void btnXsltLoad_Click(object sender, EventArgs e) {
			this.dlgSave.DefaultExt = "xsl";
			this.dlgSave.Filter = "Xsl file (*.xsl)|*.xsl";
			if (this.dlgOpen.ShowDialog() == DialogResult.OK) {
				using (StreamReader reader = new StreamReader(this.dlgOpen.FileName)) {
					this.txtXslt.Text = reader.ReadToEnd();
				}
			}
		}

		private void btnTransform_Click(object sender, EventArgs e) {
			try {
				XslCompiledTransform transform = new XslCompiledTransform();
				using (XmlReader reader = XmlReader.Create(new StringReader(this.txtXslt.Text))) {
					transform.Load(reader);
				}

				StringBuilder builder = new StringBuilder();
				XmlWriterSettings settings = new XmlWriterSettings();
				settings.CheckCharacters = transform.OutputSettings.CheckCharacters;
				settings.CloseOutput = true;
				settings.ConformanceLevel = ConformanceLevel.Auto;
				settings.Encoding = transform.OutputSettings.Encoding;
				settings.Indent = true;
				settings.IndentChars = transform.OutputSettings.IndentChars;
				settings.NewLineChars = transform.OutputSettings.NewLineChars;
				settings.NewLineHandling = transform.OutputSettings.NewLineHandling;
				settings.NewLineOnAttributes = transform.OutputSettings.NewLineOnAttributes;
				settings.OmitXmlDeclaration = transform.OutputSettings.OmitXmlDeclaration;

				using (XmlReader reader = XmlReader.Create(new StringReader(this.txtInput.Text)))
				using (XmlWriter writer = XmlWriter.Create(new StringWriter(builder), settings)) {
					transform.Transform(reader, writer);
					writer.Flush();
					this.txtOutput.Text = builder.ToString();
				}
				this.lblMessage.Text = "Ok.";
			} catch (Exception ex) {
				this.lblMessage.Text = "Error: " + ex.Message;
			}
		}

	}
}